import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn, STComponent, STData, STPage, STRes, STChange } from '@delon/abc/st';
import { SFComponent, SFSchema } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import format from 'date-fns/format';
// import { of } from 'rxjs';
// import { delay } from 'rxjs/operators';

import { SysLoginlogShowComponent } from './show/show.component';

@Component({
  selector: 'app-sys-loginlog',
  templateUrl: './loginlog.component.html'
})
export class SysLoginlogComponent implements OnInit {
  url = `api/log`;
  //@ViewChild('sf', { static: false }) private sf!: SFComponent;
  @ViewChild('st') private readonly st!: STComponent;
  searchSchema: SFSchema = {
    properties: {
      keyword: {
        type: 'string',
        title: '',
        ui: {
          placeholder: '关键词'
        }
      }
    }
  };
  columns: STColumn[] = [
    { title: 'ID', index: 'ID', className: 'text-center' },
    { title: '登录用户名', index: 'username', className: 'text-center' },
    { title: '登录信息', index: 'info', className: 'text-center' },
    { title: '登录ip', index: 'ip', className: 'text-center' },
    { title: 'ip所在地', index: 'location', className: 'text-center', default: '-' },
    {
      title: '操作时间',
      index: 'create_time',
      format: (item, _col, index) => format(new Date(item['create_time'] * 1000), 'yyyy-MM-dd HH:mm:ss'),
      className: 'text-center',
      default: '-'
    },
    {
      title: '操作',
      buttons: [
        {
          text: '查看',
          icon: 'edit',
          type: 'static',
          modal: {
            component: SysLoginlogShowComponent,
            params: (record: STData) => record,
            modalOptions: { nzMaskClosable: false }
          },
          click: 'reload'
        },
        { text: '删除', type: 'del', icon: 'delete', className: 'text-error', click: (item: any) => this.del(item.ID) }
      ]
    }
  ];

  res: STRes = {
    reName: {
      total: 'data.total',
      list: 'data.list'
    }
  };
  paginate: STPage = {
    showSize: true
  };

  constructor(private http: _HttpClient, private modal: ModalHelper) {}

  change(e: STChange) {
    console.log('change', e.loaded);
  }

  ngOnInit = (): void => {};

  /**
   * 删除
   *
   * @param id 数据id
   */
  del(id: number): void {
    this.http.delete(`api/log/${id}`).subscribe(() => {
      this.st.reload();
    });
  }
}
